package proxy.staticproxy;

import proxy.IOrderService;
import proxy.IOrderServiceImpl;
import proxy.Order;

public class OrderServiceStaticProxy {
    private IOrderService iOrderService;

    // 增强代理 精髓就在于 加了 “beforeMethod 和 afterMethod”
    public int saveOrder(Order order){
        beforeMethod(order);
        iOrderService = new IOrderServiceImpl();
        int result = iOrderService.saveOrder(order);
        afterMethod();
        return result;
    }

    public void beforeMethod(Order order){
        int userId = order.getUserId();
        // 取模 只会得到 0 或者 1
        int dbRouter = userId %2;
        System.out.println("静态代理分配到【 db" + dbRouter + "】处理数据");

        System.out.println("静态代理 before code");
    }

    public void afterMethod(){
        System.out.println("静态代理 after code");
    }
}
